class eventFactory:
  def __init__(self):
    self.events = []
  
  def add_event(self, type, time_required, state, time_activate=0):
    self.events.append(Event(type, time_required, state, time_activate))
  
  def process_events(self,time, stationF):
    for event in self.events:
      event.process_event(time, stationF)
  
  def is_failure(self):
    for event in self.events:
      if event.state == "incomplete":
        return True
      else:
        return False
    
class Event:
  def __init__(self, type, time_required, state, time_activate):
    self.time_activate = time_activate
    self.type = type
    self.time_required = time_required
    self.state = state
    # STATES:
    # active - event is active and the life drains
    # work - player is working on an event
    # incomplete - the life is below or at 0, but the event is not fulfilled
    # complete - event is fulfilled and the life is above 0 
    # inactive - event is not active and will not be processed.
    
  def process_event(self, time, stationF):
    #Activate event when time comes
    if self.time_activate <= time:
      if self.state == "inactive":
        self.state = "active"
        
    #Flag incomplete when active before time period
    if self.state == "active":
      #Oh no! Out of time!
      if time >= self.time_required:
        if self.state == "active":
          self.state = "incomplete"
          
    if stationF.numEntity == 0:
      self.state = "complete"
          
     #Keep track of damaged and intialized station events

    for station in stationF.stationList:
      if self.type == "radiation":
        if self.state == "active":
          if station.type == "ai panel" and station.event == 0:
            self.event = 1
        if self.state == "incomplete":
          if station.type == "power":
            station.damaged = 1
      
      if self.type == "breach":
        if self.state == "active":
          if station.type == "breach" and station.event == 0:
            station.event = 1
        if self.state == "incomplete":
          if station.type == "life support":
            station.damaged = 1
          if station.type == "navigation":
            station.damaged = 1
            
      if self.type == "leak":
        if self.state == "active":
          if station.type == "leak"  and station.event == 0:
            station.event = 1
        if self.state == "incomplete":
          if station.type == "life support":
            station.damaged = 1
          if station.type == "ai":
            station.damaged = 1
            
      if self.type == "hal 9000":
        if self.state == "active":
          if station.type == "ai panel" and station.event == 0:
            print "test"
            station.event = 1
        if self.state == "incomplete":
          station.damaged = 1
 
    #Keep track of complete items
    #if self.state == "active":
      #print stationF.numLeaks
      # if self.type == "leak":
        # leakFixed = 0
        # for station in stationF.stationList:
          # if station.type == "leak":
            # if station.event == 2:
              # leakFixed = leakFixed + 1
          # if leakFixed == stationF.numLeaks:
            # self.state = "complete"
   
      # if self.type == "breach":
        # for station in stationF.stationList:
          # breachesFixed = 0
          # if station.type == "breach":
            # if station.event == 2:
              # leakFixed = breachesFixed + 1
          # if breachesFixed == stationF.numBreaches:
            # self.state = "complete" 
        
 
          
      
